﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TTQT.TTQT_Code;
using System.Web.Services;

public partial class TrangGioHang : System.Web.UI.Page
{
    [WebMethod()]
    public static string dataSave(string name)
    {
        return name;
    }
    public GioHang gioHang
    {
        get
        {
            try
            {
                if (null != Session["GioHang"])
                {
                    return (GioHang)Session["GioHang"];
                }
                else
                {
                    return new GioHang();
                }
            }
            catch
            {
                return new GioHang();
            }
        }
        set { Session["GioHang"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        string type = Request.QueryString["type"];
        type = TTQT.TTQT_Code.TTQTCommon.isNull(type, "");
        string spId = Request.QueryString["id"];
        string soluong = Request.QueryString["soluong"];
        string data = Request.QueryString["data"];
        if(string.IsNullOrEmpty(type))// List san pham trong gio hang
        {
            Response.ContentType = "application/json";
            string jsonKQ = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(gioHang.sanPhamList);
            Response.Write(jsonKQ);
            return;
        }
        if (type.Equals("add")) // Them moi san pham vao gio hang
        {
            //Tham so can truyen vao: 
            //GioHang.aspx?type=add&id=id&soLuong=soluong
            try
            {
                SanPhamDAO spDao = new SanPhamDAO();
                SanPhamInfo spInfo = spDao.getById(spId);
                if (!string.IsNullOrEmpty(spInfo.Id))
                {
                    spInfo.SoLuongDatMua = TTQTCommon.ToInt32(soluong, 0);
                    GioHang gh = gioHang;
                    gh.muaThem(spInfo);
                    gioHang = gh;
                }
            }
            catch (Exception ex) { }

            Response.ContentType = "application/json";
            string jsonKQ = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(gioHang.sanPhamList);
            Response.Write(jsonKQ);
            return;
        }
        if (type.Equals("update"))//Update so luong 1 san pham trong gio hang
        {
            //Tham so can truyen vao: 
            //GioHang.aspx?type=add&id=id&soLuong=soluong
            try
            {
                
                SanPhamDAO spDao = new SanPhamDAO();
                SanPhamInfo spInfo = spDao.getById(spId);
                if (!string.IsNullOrEmpty(spInfo.Id))
                {
                    spInfo.SoLuongDatMua = TTQTCommon.ToInt32(soluong, 0);
                    GioHang gh = gioHang;
                    gh.updateSoLuong(spInfo);
                    gioHang = gh;
                }
            }catch(Exception ex){}
            
            Response.ContentType = "application/json";
            string jsonKQ = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(gioHang.sanPhamList);
            Response.Write(jsonKQ);
            return;
        }
        if (type.Equals("remove"))//Update so luong 1 san pham trong gio hang
        {
            //Tham so can truyen vao: 
            //GioHang.aspx?type=add&id=id&soLuong=soluong
            try
            {

                SanPhamDAO spDao = new SanPhamDAO();
                //SanPhamInfo spInfo = spDao.getById(spId);
                SanPhamInfo spInfo = new SanPhamInfo();
                spInfo.Id = spId;
                if (!string.IsNullOrEmpty(spInfo.Id))
                {
                    
                    GioHang gh = gioHang;
                    gh.xoaBo(spInfo);
                    gioHang = gh;
                }
            }
            catch (Exception ex) { Response.Write("2"); }

            /*Response.ContentType = "application/json";
            string jsonKQ = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(gioHang.sanPhamList);
            Response.Write(jsonKQ);*/
            Response.Write("1"); // Delete success
            return;
        }
        if (type.Equals("updatecart"))//Update so luong 1 san pham trong gio hang
        {
            //Tham so can truyen vao: 
            //GioHang.aspx?type=updatecart&data=id:soluong;id:soluong;id:soluong
            string kq = "1";
            string _id, _soluong;
            try
            {
                if(string.IsNullOrEmpty(data))
                {
                    kq = "2";
                }
                else
                {
                    //xoa het; o dau va cuoi chuoi
                    while (data.StartsWith(";"))
                    {
                        data = data.Substring(1);
                    }
                    while (data.EndsWith(";"))
                    {
                        data = data.Substring(0, data.Length - 1);
                    }
                    //Xong xoa het; o dau va cuoi chuoi
                    string[] pair = data.Split(';');
                    SanPhamDAO spDao = new SanPhamDAO();
                    GioHang gh = gioHang;
                    gh.xoaHet();
                    for(int i=0;i<pair.Length;i++)
                    {
                        if (pair[i].IndexOf(':')>0) //cap id:soluong nay hop le
                        {
                            _id = pair[i].Split(':')[0];
                            _soluong = pair[i].Split(':')[1];
                            SanPhamInfo spInfo = spDao.getById(_id);
                            spInfo.SoLuongDatMua = TTQTCommon.ToInt32(_soluong, 0);
                            gh.updateSoLuong(spInfo);
                        }
                    }
                    gioHang = gh;
                }
               
            }
            catch (Exception ex)
            {
                kq = "2";
            }

            Response.ContentType = "text/html;level=1";
            //string jsonKQ = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(gioHang.sanPhamList);
            Response.Write(kq);
            return;
        }

        if (type.Equals("neworder"))//Update so luong 1 san pham trong gio hang
        {
            string kq = "1";
            //Kiem tra xem neu gio hang rong thi tra ve 3
            if(gioHang.sanPhamList.Count<1)
            {
                kq = "0";
                Response.ContentType = "text/html;level=1";
                //string jsonKQ = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(gioHang.sanPhamList);
                Response.Write(kq);
                return;
            }
            
            SanPham_DatHangDAO inf = new SanPham_DatHangDAO();

            inf.HoTen = Request.QueryString["ord_name"];
            inf.HoTen1 = Request.QueryString["ord_sname"];

            inf.GioiTinh = Request.QueryString["ord_gender"];
            inf.GioiTinh = inf.GioiTinh.Equals("0") ? "Nữ" : "Nam";
            inf.GioiTinh1 = Request.QueryString["ord_sgender"];
            inf.GioiTinh1 = inf.GioiTinh1.Equals("0") ? "Nữ" : "Nam";

            inf.DiaChi = Request.QueryString["ord_address"];
            inf.DiaChi1 = Request.QueryString["ord_saddress"];

            inf.Email = Request.QueryString["ord_email"];
            inf.Email1 = Request.QueryString["ord_semail"];

            inf.DienThoai = Request.QueryString["ord_phone"];
            inf.DienThoai1 = Request.QueryString["ord_sphone"];
            
            inf.DiDong = Request.QueryString["ord_mobile"];
            inf.DiDong1 = Request.QueryString["ord_smobile"];

            inf.Fax = Request.QueryString["ord_fax"];
            inf.Fax1 = Request.QueryString["ord_sfax"];

            inf.GhiChu1 = Request.QueryString["ord_sotherinfo"];

            inf.ThoiGianVanChuyen = TTQTCommon.convertDateString(Request.QueryString["ord_date"]);
            inf.HinhThucVanChuyen = Request.QueryString["ord_delivery"];
            if (inf.HinhThucVanChuyen.Equals("1"))
                inf.HinhThucVanChuyen = "Đến địa chỉ người nhận";
            if (inf.HinhThucVanChuyen.Equals("2"))
                inf.HinhThucVanChuyen = "Khách đến nhận hàng";
            if (inf.HinhThucVanChuyen.Equals("3"))
                inf.HinhThucVanChuyen = "Qua bưu điện";
            if (inf.HinhThucVanChuyen.Equals("4"))
                inf.HinhThucVanChuyen = "Hình thức khác";

            inf.HinhThucThanhToan = Request.QueryString["ord_payment"];
            if (inf.HinhThucThanhToan.Equals("0"))
                inf.HinhThucThanhToan = "Tiền mặt";
            if (inf.HinhThucThanhToan.Equals("1"))
                inf.HinhThucThanhToan = "Thẻ ATM";
            if (inf.HinhThucThanhToan.Equals("2"))
                inf.HinhThucThanhToan = "Chuyển khoản";

            inf.TongTien = gioHang.tongTien().Replace(",","");
            inf.Id = inf.insert().ToString();
            if(inf.getId()>0)
            {
                //Them cac thong tin san pham trong gio hang
                inf.insertDetail(inf.Id,gioHang.sanPhamList);
                kq = "1";
                Session.Remove("GioHang");
                //Xây dựng nội dung email
                StringBuilder sb=new StringBuilder();

            }
            else
            {
                kq = "2";
            }
            //Tham so can truyen vao: 
            //GioHang.aspx?type=neworder&data=id:soluong;id:soluong;id:soluong
            

            Response.ContentType = "text/html;level=1";
            //string jsonKQ = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(gioHang.sanPhamList);
            Response.Write(kq);
            return;
        }
    }
}
